// dom ready / closure
$(function() {

	// some variables
	var e = {}, arr = "event_tooltip,event_loader,temp".split(",");
	
	// create divs
	$.each(arr, function(i) {
		e[arr[i].replace("event_", "")] = $("<div>").addClass(arr[i]);
	});
	
	// build markup and hide
	e.tooltip.append(e.loader).hide();
	
	// loop through the link
	$('#calendars div.link a').each(function() {
	
		// cache root element and remove its title attribute
		var $root = $(this).removeAttr('title');
		
		// bind hover effect
		$root.hover(function() {
			
			// add our tooltip in
			var $tip = e.tooltip.clone().appendTo(this);
			
			// then place it (damn IE we could do this better otherwise)
			$tip.css({
				left:$root.position().left+$root.outerWidth()+'px',
				top:$root.position().top+'px',
				display:'block'
			});
			
			// do our ajax query
			$.ajax({
				url: '/shared/jsp/_day_events.jsp?strlang='+$('html').eq(0).attr('lang')+'&lang='+$('#orglang').attr('content')+'&org='+$('#orgref').attr('content')+'&'+$root.attr('href').replace('/'+$('html').eq(0).attr('lang')+'/events/day?',''),
				cache: true,
				success: function(html) {
					$tip.html(html);
					$tip.children('a').click(function(ev) {
						ev.preventDefault();
						window.location = $(this).attr('href');
					});
				},
				error: function() {
					$tip.html('Error retrieving data');
				}
			});
			
		}, function() {
			$(this).children().remove();
		});
	});
});
